package com.hackordie.daos;

import com.hackordie.commons.game.player.UserComponent;
import com.hackordie.commons.hardware.components.AComponent;
import com.hackordie.constants.EComponentsType;
import com.hackordie.daos.mappers.FirewallMapper;
import com.hackordie.daos.mappers.HardDiskMapper;
import com.hackordie.daos.mappers.NetworkCardMapper;
import com.hackordie.daos.mappers.PowerSupplyMapper;
import com.hackordie.daos.mappers.ProcessorMapper;
import com.hackordie.daos.mappers.RamMemoryMapper;
import com.hackordie.daos.mappers.UserComponentMapper;

public class UserComponentsDao extends ACommonDao<UserComponent> {

    @Override
    public boolean create(UserComponent entity) {
        final String SQL = "INSERT INTO userComponents (idUser, " +
        		                                       "idComponentData, " +
        		                                       "mounted, " +
        		                                       "componentType, " +
        		                                       "life) " +
        		           "VALUES (" + entity.getIdUser() + ", " +
        		                        entity.getComponent().getIdComponent()+ ", " +
        		                        entity.isMounted()+ ", " +
        		                  "'" + entity.getComponentType()+ "', " +
        		                        entity.getLife() + ")";
   
        //update devuelve numero de filas afectadas
        //debe ser 1        
        if(jdbcTemplate.update(SQL) == 1) {  
            return true;
        }
        return true;
    }

    @Override
    public boolean delete(int id) {
        final String SQL = "DELETE FROM userComponents WHERE idCOmponent = " + id;
        
        if(jdbcTemplate.update(SQL) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(UserComponent entity) {
        final String SQL = "UPDATE userComponents " + 
                               "SET idUser = " + entity.getIdUser() + ", " + 
                               "SET idComponentData = " + entity.getComponent().getIdComponent() + ", " + 
                               "SET mounted = " + entity.isMounted() + ", " + 
                               "SET componentType = " + entity.getComponentType() + ", " + 
                               "SET life = " + entity.getLife() + ", " +
                           "WHERE idComponent = " + entity.getIdComponentOnBag();
        
        if(jdbcTemplate.update(SQL) == 1) {
            return true;
        }        
        return false;
    }

    @Override
    public UserComponent[] getAll() {
        // TODO Auto-generated method stub
        //TODO quisas innecesaria... remplazar por getAll(idUser)
        return null;
    }

    @Override
    public UserComponent get(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    
    
    //datos de la tabla userComponents
    public UserComponent[] getAllUserComponents(int idUser) {
        final String SQL = "SELECT * " +
                           "FROM userComponents " +
                           "WHERE idUser = " + idUser;
        
        return jdbcTemplate.query(SQL, new UserComponentMapper()).toArray(new UserComponent[] {});
        
    }    
    
    public AComponent getComponentOnBag(int idComponentOnBag,
            EComponentsType componentType) {
        
        String SQL = "SELECT components.idComponent, " +
                             "name, " +
                             "energyTurnOn, " +
                             "prize, ";
        
        switch(componentType) {
        case Firewall:
            SQL += "difficultAccess " +
                   "FROM components, firewalls " + 
                   "WHERE components.idComponent = firewalls.idComponent ";
            break;
        case NetworkCard:
            SQL += "speedConection " +
                   "FROM components, networkCards " + 
                   "WHERE components.idComponent = networkCards.idComponent "; 
            break;
        case HardDisk:
            SQL += "capacity " +
                   "FROM components, hardDisks " + 
                   "WHERE components.idComponent = hardDisks.idComponent "; 
            break;
        case PowerSupply:
            SQL += "capacity " +
                   "FROM components, powerSupplies " + 
                   "WHERE components.idComponent = powerSupplies.idComponent "; 
            break;
        case Processor:
            SQL += "speedForCore, cores" + 
                   "FROM components, processors" + 
                   "WHERE components.idComponent = processors.idComponent ";
            break;        
        case RamMemory:
            SQL += "capacity " +
                   "FROM components, rams" + 
                   "WHERE components.idComponent = rams.idComponent ";
            break;        
        }
        
        SQL += "AND components.idComponent = (SELECT idComponentData " +
                                             "FROM userComponents " +
                                             "WHERE idComponent = " + idComponentOnBag + ")";
        
        
        switch(componentType) {
        case Firewall:
            return jdbcTemplate.queryForObject(SQL, new FirewallMapper());
        case HardDisk:
            return jdbcTemplate.queryForObject(SQL, new HardDiskMapper());
        case NetworkCard:
            return jdbcTemplate.queryForObject(SQL, new NetworkCardMapper());
        case PowerSupply:
            return jdbcTemplate.queryForObject(SQL, new PowerSupplyMapper());
        case Processor:
            return jdbcTemplate.queryForObject(SQL, new ProcessorMapper());
        case RamMemory:  
            return jdbcTemplate.queryForObject(SQL, new RamMemoryMapper());
        }
        
        return null;
    }
}
